//
//  CustomNavigationCtrl.m
//

#import "CustomNavigationCtrl.h"
#import "UserDefaultsHelper.h"


@interface CustomNavigationCtrl ()<UIGestureRecognizerDelegate,UINavigationControllerDelegate,UIAlertViewDelegate>
@end

@implementation CustomNavigationCtrl

- (void)viewDidLoad {
    [super viewDidLoad];
    __weak CustomNavigationCtrl *weakSelf = self;
//    if ([self respondsToSelector:@selector(interactivePopGestureRecognizer)])
//    {
    
    NSLog(@"%@",self.interactivePopGestureRecognizer.delegate);
        self.interactivePopGestureRecognizer.delegate = self;
        self.delegate = weakSelf;
//    }
}



+(void)initialize{
    //设置导航栏的顶部文字颜色与大小
    UINavigationBar *appearance = [UINavigationBar appearance];
    // 设置文字属性
    NSMutableDictionary *textAttrs = [NSMutableDictionary dictionary];
    textAttrs[NSForegroundColorAttributeName] = [UIColor whiteColor];
    textAttrs[NSFontAttributeName] = CustomNavigationTitleFont;
    // UIOffsetZero是结构体, 只要包装成NSValue对象, 才能放进字典\数组中
    textAttrs[UITextAttributeTextShadowOffset] = [NSValue valueWithUIOffset:UIOffsetZero];
    [appearance setTitleTextAttributes:textAttrs];
}

- (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated
{
    //如果在push过程中触发手势滑动返回，会导致导航栏崩溃（从日志中可以看出）。针对这个问题，我们需要在pop过程禁用手势滑动返回功能
    if ([self respondsToSelector:@selector(interactivePopGestureRecognizer)])
        self.interactivePopGestureRecognizer.enabled = NO;
    if (self.viewControllers.count > 0) { // 如果现在push的不是栈底控制器(最先push进来的那个控制器)
        viewController.hidesBottomBarWhenPushed = YES;
        // 设置导航栏按钮
//        viewController.navigationItem.leftBarButtonItem = [UIBarButtonItem itemWithImageName:@"backbtn" highImageName:@"backbtn" target:self action:@selector(back)];
        viewController.navigationItem.leftBarButtonItem = [UIBarButtonItem backItemWithTarget:self action:@selector(back)];
    }
    [super pushViewController:viewController animated:animated];
}

- (void)back
{
    [self popViewControllerAnimated:YES];
}

- (void)navigationController:(UINavigationController *)navigationController
       didShowViewController:(UIViewController *)viewController
                    animated:(BOOL)animate{
    if ([self respondsToSelector:@selector(interactivePopGestureRecognizer)]){
        self.interactivePopGestureRecognizer.enabled = YES;
    }
}

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
}

-(void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];

}


-(BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer{
    //判断当前是否是第一个控制器，如果是的话则不手势识别
    if(self.viewControllers.count==1){
        return NO;
    }
    return YES;
}

@end
